﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Contexts;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.CompilerServices;

namespace Infov1.Models
{
    [SugarTable("k_admin")] // 绑定表名
    public class AdminModel
    {
        [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
        public int admin_id { get; set; }

        [SugarColumn(ColumnName = "username")] 
        public string Username { get; set; }

        [SugarColumn(ColumnName = "password")] // 保持不变，因为已经正确映射
        public string Password { get; set; }
        [SugarColumn(ColumnName = "name")] // 保持不变，因为已经正确映射
        public string Name { get; set; }

        // 其他字段...
        // 创建一个用于返回验证结果的类
        public class ValidationResult
        {
            public bool IsValid { get; set; }
            public int AdminId { get; set; }
            public string Name { get; set; }
        }
// 验证用户方法
        public static async Task<ValidationResult> ValidateUserAsync(string username, string password)
        {
            return await Task.Run(() =>
            {
                using (var db = DbContext.GetConnection())
                {
                    if (db == null) return new ValidationResult 
                    { 
                        IsValid = false, 
                        AdminId = 0, 
                        Name = null 
                    };

                    var user = db.Queryable<AdminModel>()
                        .Where(u => u.Username == username && u.Password == password)
                        .First();

                    if (user != null)
                    {
                        return new ValidationResult
                        {
                            IsValid = true,
                            AdminId = user.admin_id,
                            Name = user.Name
                        };
                    }
                    return new ValidationResult 
                    { 
                        IsValid = false, 
                        AdminId = 0,
                        Name = null 
                    };
                }
            });
        }
    }
}
